var express      = require('express'),
    app          = express(),
    bodyParser   = require('body-parser'),
    compress     = require('compression'),
    cors         = require('cors'),
    favicon      = require('static-favicon'),
    //http         = require('http'),
    isDev        = app.get('env') === 'development',
    logger       = require('morgan'),
    
    port         = process.env["port"] || 7070, //服务端口号
    //自定义功能,错误处理
    errorHandler = require('./utils/errorHandler')();

app.use(compress()); 									// Compress response data with gzip
app.use(logger('dev')); 							 	// logger
app.use(bodyParser.urlencoded({extended: true}));    	//body parser, json, and url encoding
app.use(bodyParser.json());						     	//body parser, json, and url encoding
app.use(cors()); 										//开启CORS访问
app.use('/', express.static(__dirname + '/static')); 	//支持静态文件,目录static,默认文件为index.html,默认路径为/

//HTTP异常处理配置errorHandler.js
app.use(errorHandler.init);
//HTTP路由配置
require('./routes/config')(app);

//开发测试服务是否正常
if(isDev){
    app.get('/test', function(req, res, next) {
        console.log(req.body);
        res.send('ping');
    });
}

//创建服务
//var server = http.createServer(app);
//启动网络监听，在控制台输出日志
app.listen(port, function(){
    console.log('Express server run ok !');
    console.log('port = ' + port);
    console.log('env = '+ app.get('env') +
        '\n__dirname = ' + __dirname  +
        '\nprocess.cwd = ' + process.cwd() );
    console.log('url = http://127.0.0.1:' + port);
    console.log('running ...');
});